package com.jwgf.smarowindingmachine_unity.ui;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.jwgf.Half485.fxthreadpool.FxCommandManager;
import com.jwgf.Half485.fxthreadpool.FxPLCData;
import com.jwgf.Half485.modbusthreadpool.ModbusCommandManager;
import com.jwgf.Half485.modbusthreadpool.PLCData;
import com.jwgf.common.Utility;
import com.jwgf.common.ViewCache;
import com.jwgf.smarowindingmachine_unity.R;
import com.jwgf.smarowindingmachine_unity.logic.CommandGenerator;
import com.jwgf.smarowindingmachine_unity.logic.MachineParameterSharedPreference;
import com.jwgf.smarowindingmachine_unity.logic.SmaroWindingMachineDataAccessRules;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class ParameterSettingMachineParameterFragment extends BaseFragment
        implements OnClickListener, OnEditorActionListener, OnFocusChangeListener {
    public static final String fragmentTag = "ParameterSettingMachineParameterFragment";
    static final String INPUT_ERROR = "Error";
    private MachineParameterSharedPreference sp = MachineParameterSharedPreference.getInstance();

    private EditText time_edit;
    private EditText times_edit;
    private EditText interval_time_edit;
    private EditText machine_cycle_num_edit;
    private EditText minus_pressure_edit;
    private EditText fork_off_time_edit;
    private EditText fork_on_time_edit;
    private EditText empty_tube_belt_action_interval_edit;

    private ImageButton btn_transport;
    private ImageButton btn_machine_type;
    private ImageButton btn_spinningin_type;
    private ImageButton btn_trans_type;
    private ImageButton btn_empty_tube_belt_transport_type;
    private ImageButton btn_flat_belt_sensor;
    private ImageButton btn_direct_current;
    private ImageButton btn_stop_type;

    private TextView transport_value;
    private TextView machine_type_value;
    private TextView spinningin_type_value;
    private TextView trans_type_value;
    private TextView direct_current_value;
    private TextView stop_type_value;

    private RelativeLayout spinning_layout;
    private RelativeLayout trans_type_layout;
    private RelativeLayout trans_type_time_layout;
    private RelativeLayout trans_type_count_layout;
    private TextView empty_tube_belt_transport_type_value;
    private TextView flat_belt_sensor_value;

    //左手车功能
    private RelativeLayout fork_off_time_layout;
    private RelativeLayout fork_on_time_layout;

    private RelativeLayout empty_tube_belt_transport_type_layout;
    private RelativeLayout empty_tube_belt_action_interval_layout;
    private RelativeLayout flat_belt_sensor_layout;

    private View thisView = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = ViewCache.getInstance().getView(R.layout.fragment_parameter_setting_machine_parameter);
//        View v = inflater.inflate(R.layout.fragment_parameter_setting_machine_parameter, container, false);
        thisView = v;
        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initialViews(thisView);
        EventBus.getDefault().register(this);
        updateViews();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden == true) {
            EventBus.getDefault().unregister(this);
        } else {
            updateViews();
            EventBus.getDefault().register(this);
        }
    }

    private void initialViews(View v) {
        spinning_layout = (RelativeLayout) v.findViewById(R.id.spinninginlayout);
        trans_type_layout = v.findViewById(R.id.transtypelayout_up);
        trans_type_time_layout = v.findViewById(R.id.transtype_timelayout);
        trans_type_count_layout = v.findViewById(R.id.transtype_countlayout);
        btn_transport = (ImageButton) v.findViewById(R.id.btn_transport);
        btn_transport.setOnClickListener(this);
        btn_machine_type = (ImageButton) v.findViewById(R.id.btn_machine_type);
        btn_machine_type.setOnClickListener(this);
        btn_spinningin_type = (ImageButton) v.findViewById(R.id.btn_spinningin_type);
        btn_spinningin_type.setOnClickListener(this);
        btn_trans_type = (ImageButton) v.findViewById(R.id.btn_trans_type);
        btn_trans_type.setOnClickListener(this);
        btn_flat_belt_sensor = (ImageButton) v.findViewById(R.id.btn_flat_belt_sensor);
        btn_flat_belt_sensor.setOnClickListener(this);
        btn_direct_current = v.findViewById(R.id.btn_direct_current);
        btn_direct_current.setOnClickListener(this);
        btn_stop_type = v.findViewById(R.id.btn_stop_type);
        btn_stop_type.setOnClickListener(this);

        transport_value = (TextView) v.findViewById(R.id.transport_value);
        machine_type_value = (TextView) v.findViewById(R.id.machine_type_value);
        spinningin_type_value = (TextView) v.findViewById(R.id.spinningin_type_value);
        trans_type_value = (TextView) v.findViewById(R.id.trans_type_value);
        direct_current_value = v.findViewById(R.id.direct_current_value);
        stop_type_value = v.findViewById(R.id.stop_type_value);

        time_edit = initEdittextOnCreate(R.id.trans_time_value);
        times_edit = initEdittextOnCreate(R.id.times_value);
        interval_time_edit = initEdittextOnCreate(R.id.interval_time_value);
        machine_cycle_num_edit = initEdittextOnCreate(R.id.machine_cycle_num_value);
        minus_pressure_edit = initEdittextOnCreate(R.id.minus_pressure_value);

        fork_off_time_layout = v.findViewById(R.id.fork_off_time_layout);
        fork_on_time_layout = v.findViewById(R.id.fork_on_time_layout);
        empty_tube_belt_transport_type_layout = v.findViewById(R.id.empty_tube_belt_transport_type_layout);
        empty_tube_belt_action_interval_layout = v.findViewById(R.id.empty_tube_belt_action_interval_layout);
        flat_belt_sensor_layout = v.findViewById(R.id.flat_belt_sensor_layout);

        btn_empty_tube_belt_transport_type = v.findViewById(R.id.btn_empty_tube_belt_transport_type);
        empty_tube_belt_transport_type_value = v.findViewById(R.id.empty_tube_belt_transport_type_value);
        flat_belt_sensor_value = v.findViewById(R.id.flat_belt_sensor_value);
        btn_empty_tube_belt_transport_type.setOnClickListener(this);
        fork_off_time_edit = initEdittextOnCreate(R.id.fork_off_time_value);
        fork_on_time_edit = initEdittextOnCreate(R.id.fork_on_time_value);
        empty_tube_belt_action_interval_edit = initEdittextOnCreate(R.id.empty_tube_belt_action_interval_value);
    }

    private void updateViews() {
        time_edit.setText(sp.getValue(MachineParameterSharedPreference.MACHINE_TIME, "1"));
        times_edit.setText(sp.getValue(MachineParameterSharedPreference.MACHINE_TIMES, "1"));
        interval_time_edit.setText(sp.getValue(MachineParameterSharedPreference.MACHINE_INTERVAL_TIME, "0"));
        machine_cycle_num_edit.setText(sp.getValue(MachineParameterSharedPreference.MACHINE_CYCLE_NUM, "0"));
        minus_pressure_edit.setText(sp.getValue(MachineParameterSharedPreference.MACHINE_MINUS_PRESSURE, "45"));

        if (sp.getValue(MachineParameterSharedPreference.MACHINE_TRANSPORT, "0").equals("0")) {
            transport_value.setText(R.string.Nose);
        } else if (sp.getValue(MachineParameterSharedPreference.MACHINE_TRANSPORT, "0").equals("1")) {
            transport_value.setText(R.string.Tail);
        } else {
            transport_value.setText(R.string.None);
        }

        if (sp.getValue(MachineParameterSharedPreference.MACHINE_MACHINE_TYPE, "0").equals("0")) {
            machine_type_value.setText(R.string.Artificial_doffing);
            trans_type_layout.setVisibility(View.VISIBLE);
            trans_type_time_layout.setVisibility(View.VISIBLE);
            trans_type_count_layout.setVisibility(View.VISIBLE);
            spinning_layout.setVisibility(View.INVISIBLE);
        } else if (sp.getValue(MachineParameterSharedPreference.MACHINE_MACHINE_TYPE, "0").equals("1")) {
            machine_type_value.setText(R.string.Automatic_doffing);
            trans_type_layout.setVisibility(View.VISIBLE);
            trans_type_time_layout.setVisibility(View.VISIBLE);
            trans_type_count_layout.setVisibility(View.VISIBLE);
            spinning_layout.setVisibility(View.INVISIBLE);
        } else if (sp.getValue(MachineParameterSharedPreference.MACHINE_MACHINE_TYPE, "0").equals("2")) {
            machine_type_value.setText(R.string.Automatic_feeding_tube);
            trans_type_layout.setVisibility(View.INVISIBLE);
            trans_type_time_layout.setVisibility(View.INVISIBLE);
            trans_type_count_layout.setVisibility(View.INVISIBLE);
            spinning_layout.setVisibility(View.VISIBLE);
        } else if (sp.getValue(MachineParameterSharedPreference.MACHINE_MACHINE_TYPE, "0").equals("3")) {
            machine_type_value.setText(R.string.combined_spinning_winding);
            trans_type_layout.setVisibility(View.INVISIBLE);
            trans_type_time_layout.setVisibility(View.INVISIBLE);
            trans_type_count_layout.setVisibility(View.INVISIBLE);
            spinning_layout.setVisibility(View.INVISIBLE);
        } else if (sp.getValue(MachineParameterSharedPreference.MACHINE_MACHINE_TYPE, "0").equals("4")) {
            machine_type_value.setText(R.string.Tube_of_rewinding);
            trans_type_layout.setVisibility(View.VISIBLE);
            trans_type_time_layout.setVisibility(View.VISIBLE);
            trans_type_count_layout.setVisibility(View.VISIBLE);
            spinning_layout.setVisibility(View.INVISIBLE);
        } else if (sp.getValue(MachineParameterSharedPreference.MACHINE_MACHINE_TYPE, "0").equals("5")) {
            machine_type_value.setText(R.string.Big_yarn_library);
            trans_type_layout.setVisibility(View.INVISIBLE);
            trans_type_time_layout.setVisibility(View.INVISIBLE);
            trans_type_count_layout.setVisibility(View.INVISIBLE);
            spinning_layout.setVisibility(View.VISIBLE);
        }

        if (sp.getValue(MachineParameterSharedPreference.MACHINE_TRANS_TYPE, "0").equals("0")) {
            trans_type_value.setText(R.string.continuity);
        } else {
            trans_type_value.setText(R.string.intermittent);
        }
        if (sp.getValue(MachineParameterSharedPreference.MACHINE_SPINNINGIN_TYPE, "0").equals("0")) {
            spinningin_type_value.setText(R.string.three_spinningin);
        } else {
            spinningin_type_value.setText(R.string.two_spinningin);
        }

        String[] direct_current_content = getResources().getStringArray(R.array.yes_no_choose);
        if (sp.getValue(MachineParameterSharedPreference.MACHINE_DIRECT_CURRENT, "0").equals("0")) {
            direct_current_value.setText(direct_current_content[0]);
        } else {
            direct_current_value.setText(direct_current_content[1]);
        }

        String[] content = getResources().getStringArray(R.array.trans_type);
        if (sp.getValue(MachineParameterSharedPreference.MACHINE_EMPTY_TUBE_BELT_TRANSPORT_TYPE).equals("0")) {
            empty_tube_belt_transport_type_value.setText(content[0]);
            empty_tube_belt_action_interval_layout.setVisibility(View.INVISIBLE);
        } else {
            empty_tube_belt_transport_type_value.setText(content[1]);
            empty_tube_belt_action_interval_layout.setVisibility(View.VISIBLE);
            empty_tube_belt_action_interval_edit.setText(sp.getValue(MachineParameterSharedPreference.MACHINE_EMPTY_TUBE_BELT_ACTION_INTERVAL));
        }
        empty_tube_belt_action_interval_edit.setText(sp.getValue(MachineParameterSharedPreference.MACHINE_EMPTY_TUBE_BELT_ACTION_INTERVAL));

        String[] flat_belt_sensor_content = getResources().getStringArray(R.array.have_none);
        if (sp.getValue(MachineParameterSharedPreference.MACHINE_FLAT_BELT_SENSOR, "0").equals("0")) {
            flat_belt_sensor_value.setText(flat_belt_sensor_content[0]);
        } else {
            flat_belt_sensor_value.setText(flat_belt_sensor_content[1]);
        }

        // 左手车
        if (sp.getValue(MachineParameterSharedPreference.SYSTEM_MACHINE_DRIVE, "0").equals("0")
                && sp.getValue(MachineParameterSharedPreference.MACHINE_MACHINE_TYPE, "0").equals("5")) {
            fork_off_time_layout.setVisibility(View.VISIBLE);
            fork_on_time_layout.setVisibility(View.VISIBLE);
            fork_off_time_edit.setText(sp.getValue(MachineParameterSharedPreference.MACHINE_PULL_FORK_OFF_TIME));
            fork_on_time_edit.setText(sp.getValue(MachineParameterSharedPreference.MACHINE_PULL_FORK_ON_TIME));

        } else {
            fork_off_time_layout.setVisibility(View.INVISIBLE);
            fork_on_time_layout.setVisibility(View.INVISIBLE);
        }

        if (sp.getValue(MachineParameterSharedPreference.PLC_TYPE, "0").equals("1")) {
            fork_off_time_layout.setVisibility(View.GONE);
            fork_on_time_layout.setVisibility(View.GONE);
            empty_tube_belt_transport_type_layout.setVisibility(View.GONE);
            empty_tube_belt_action_interval_layout.setVisibility(View.GONE);
            flat_belt_sensor_layout.setVisibility(View.GONE);
        }

        String[] stop_type_array = getResources().getStringArray(R.array.stop_type);
        int stop_type = Integer.parseInt(sp.getValue(MachineParameterSharedPreference.STOP_TYPE, "0"));
        stop_type_value.setText(stop_type_array[stop_type]);
    }

    private EditText initEdittextOnCreate(int id) {
        EditText editText = (EditText) getActivity().findViewById(id);
        editText.setSelectAllOnFocus(true);
        editText.setOnFocusChangeListener(this);
        editText.setOnEditorActionListener(this);
        editText.setBackgroundDrawable(null);
        return editText;
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        int id = v.getId();
        switch (id) {
            case R.id.trans_time_value:
                if (!hasFocus) {
                    time_edit.setText(sp.getValue(MachineParameterSharedPreference.MACHINE_TIME, "1"));
                }
                break;
            case R.id.times_value:
                if (!hasFocus) {
                    times_edit.setText(sp.getValue(MachineParameterSharedPreference.MACHINE_TIMES, "1"));
                }
                break;
            case R.id.interval_time_value:
                if (!hasFocus) {
                    interval_time_edit.setText(sp.getValue(MachineParameterSharedPreference.MACHINE_INTERVAL_TIME, "0"));
                }
                break;
            case R.id.machine_cycle_num_value:
                if (!hasFocus) {
                    machine_cycle_num_edit.setText(sp.getValue(MachineParameterSharedPreference.MACHINE_CYCLE_NUM, "0"));
                }
                break;
            case R.id.minus_pressure_value:
                if (!hasFocus) {
                    minus_pressure_edit.setText(sp.getValue(MachineParameterSharedPreference.MACHINE_MINUS_PRESSURE, "45"));
                }
                break;
            case R.id.fork_off_time_value:
                if (!hasFocus) {
                    fork_off_time_edit.setText(sp.getValue(MachineParameterSharedPreference.MACHINE_PULL_FORK_OFF_TIME, "1"));
                }
                break;
            case R.id.fork_on_time_value:
                if (!hasFocus) {
                    fork_off_time_edit.setText(sp.getValue(MachineParameterSharedPreference.MACHINE_PULL_FORK_ON_TIME, "1"));
                }
                break;
            case R.id.empty_tube_belt_action_interval_value:
                if (!hasFocus) {
                    empty_tube_belt_action_interval_edit.setText(sp.getValue(MachineParameterSharedPreference.MACHINE_EMPTY_TUBE_BELT_ACTION_INTERVAL, "1"));
                }
                break;
        }

    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        int id = v.getId();
        if ((actionId == EditorInfo.IME_ACTION_DONE) || (event != null && event.getAction() == KeyEvent.ACTION_DOWN)) {
            if (v.getText().length() == 0) {
                return false;
            } else {
                boolean isValid = false;
                String key = "";
                String valueString = v.getText().toString();
                byte[] datai = new byte[2];
                switch (id) {
                    case R.id.machine_cycle_num_value: {
                        key = MachineParameterSharedPreference.MACHINE_CYCLE_NUM;
                        isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, valueString);
                        if (isValid) {
                            int value = Integer.parseInt(valueString) * 2;
                            datai[0] = (byte) ((value & 0xff00) >> 8);
                            datai[1] = (byte) (value & 0xff);
                            if (sp.getValue(MachineParameterSharedPreference.PLC_TYPE, "0").equals("0")) {
                                ModbusCommandManager.getInstance().putPriorityCmd(CommandGenerator.createWriteCommand(
                                        (byte) 1, 0, datai));//720
                            } else {
                                FxCommandManager.getInstance().putPriorityCmd(CommandGenerator.createWriteCommand(
                                        (byte) 1, 0, datai));//720
                            }

                            sp.setValue(key, valueString);
                        }
                        break;
                    }
                    case R.id.interval_time_value: {
                        key = MachineParameterSharedPreference.MACHINE_INTERVAL_TIME;
                        isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, valueString);
                        if (isValid) {
                            int value = Integer.parseInt(valueString);
                            datai[0] = (byte) ((value & 0xff00) >> 8);
                            datai[1] = (byte) (value & 0xff);
                            if (sp.getValue(MachineParameterSharedPreference.PLC_TYPE, "0").equals("0")) {
                                ModbusCommandManager.getInstance()
                                        .putPriorityCmd(CommandGenerator.createWriteCommand(
                                                (byte) 1, 1, datai));//721
                            } else {
                                FxCommandManager.getInstance()
                                        .putPriorityCmd(CommandGenerator.createWriteCommand(
                                                (byte) 1, 1, datai));//721
                            }

                            sp.setValue(key, valueString);
                        }
                        break;
                    }
                    case R.id.minus_pressure_value: {
                        key = MachineParameterSharedPreference.MACHINE_MINUS_PRESSURE;
                        isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, valueString);
                        if (isValid) {
                            int value = Integer.parseInt(valueString) * 40;
                            datai[0] = (byte) ((value & 0xff00) >> 8);
                            datai[1] = (byte) (value & 0xff);
                            if (sp.getValue(MachineParameterSharedPreference.PLC_TYPE, "0").equals("0")) {
                                ModbusCommandManager.getInstance()
                                        .putPriorityCmd(CommandGenerator.createWriteCommand(
                                                (byte) 1, 2, datai));//722
                            } else {
                                FxCommandManager.getInstance()
                                        .putPriorityCmd(CommandGenerator.createWriteCommand(
                                                (byte) 1, 2, datai));//722
                            }

                            sp.setValue(key, valueString);
                        }
                        break;
                    }
                    case R.id.times_value: {
                        key = MachineParameterSharedPreference.MACHINE_TIMES;
                        isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, valueString);
                        if (isValid) {
                            int value = Integer.parseInt(valueString);
                            datai[0] = (byte) ((value & 0xff00) >> 8);
                            datai[1] = (byte) (value & 0xff);
                            if (sp.getValue(MachineParameterSharedPreference.PLC_TYPE, "0").equals("0")) {
                                ModbusCommandManager.getInstance()
                                        .putPriorityCmd(CommandGenerator.createWriteCommand(
                                                (byte) 1, 4, datai));//724
                            } else {
                                FxCommandManager.getInstance()
                                        .putPriorityCmd(CommandGenerator.createWriteCommand(
                                                (byte) 1, 4, datai));//724
                            }

                            sp.setValue(key, valueString);
                        }
                        break;
                    }
                    case R.id.trans_time_value: {
                        key = MachineParameterSharedPreference.MACHINE_TIME;
                        isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, valueString);
                        if (isValid) {
                            int value = (int) (Float.parseFloat(valueString) * 60);
                            datai[0] = (byte) ((value & 0xff00) >> 8);
                            datai[1] = (byte) (value & 0xff);
                            if (sp.getValue(MachineParameterSharedPreference.PLC_TYPE, "0").equals("0")) {
                                ModbusCommandManager.getInstance().putPriorityCmd(CommandGenerator.createWriteCommand(
                                        (byte) 1, 5, datai));//725
                            } else {
                                FxCommandManager.getInstance().putPriorityCmd(CommandGenerator.createWriteCommand(
                                        (byte) 1, 5, datai));
                            }

                            sp.setValue(key, valueString);
                        }
                        break;
                    }
                    case R.id.fork_off_time_value: {
                        key = MachineParameterSharedPreference.MACHINE_PULL_FORK_OFF_TIME;
                        isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, valueString);
                        if (isValid) {
                            int value = Integer.parseInt(valueString);
                            datai[0] = (byte) ((value & 0xff00) >> 8);
                            datai[1] = (byte) (value & 0xff);
                            ModbusCommandManager.getInstance()
                                    .putPriorityCmd(CommandGenerator.createWriteCommand(
                                            (byte) 1, 12, datai));
                            sp.setValue(key, valueString);
                        }
                        break;
                    }
                    case R.id.fork_on_time_value: {
                        key = MachineParameterSharedPreference.MACHINE_PULL_FORK_ON_TIME;
                        isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, valueString);
                        if (isValid) {
                            int value = Integer.parseInt(valueString);
                            datai[0] = (byte) ((value & 0xff00) >> 8);
                            datai[1] = (byte) (value & 0xff);
                            ModbusCommandManager.getInstance()
                                    .putPriorityCmd(CommandGenerator.createWriteCommand(
                                            (byte) 1, 13, datai));
                            sp.setValue(key, valueString);
                        }
                        break;
                    }
                    case R.id.empty_tube_belt_action_interval_value: {
                        key = MachineParameterSharedPreference.MACHINE_EMPTY_TUBE_BELT_ACTION_INTERVAL;
                        isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, valueString);
                        if (isValid) {
                            int value = Integer.parseInt(valueString);
                            datai[0] = (byte) ((value & 0xff00) >> 8);
                            datai[1] = (byte) (value & 0xff);
                            ModbusCommandManager.getInstance()
                                    .putPriorityCmd(CommandGenerator.createWriteCommand(
                                            (byte) 1, 9, datai));
                            sp.setValue(key, valueString);
                        }
                        break;
                    }
                    default:
                        break;
                }
                if (!isValid) {
                    String max = SmaroWindingMachineDataAccessRules.getInstance().getDataAccessRule(key).getMax();
                    String min = SmaroWindingMachineDataAccessRules.getInstance().getDataAccessRule(key).getMin();
                    String warning = getResources().getString(R.string.range_warning);
                    warning = String.format(warning, min, max);
                    WarningDialog.show(getActivity(), warning);
                    updateViews();
                }
            }
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_transport: {
                final String[] content = getResources().getStringArray(R.array.transport);
                ListDialogFragment listDialog = new ListDialogFragment();
                Bundle bd = new Bundle();
                bd.putStringArray(ListDialogFragment.ARRAY, content);
                listDialog.setArguments(bd);
                listDialog.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                        transport_value.setText(content[position]);
                        sp.setValue(MachineParameterSharedPreference.MACHINE_TRANSPORT, String.valueOf(position));
                    }
                });
                listDialog.show(getFragmentManager(), "listDialog");
                break;
            }

            case R.id.btn_machine_type: {
                final String[] content = getResources().getStringArray(R.array.machine_type);
                ListDialogFragment listDialog = new ListDialogFragment();
                Bundle bd = new Bundle();
                bd.putStringArray(ListDialogFragment.ARRAY, content);
                listDialog.setArguments(bd);
                listDialog.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        machine_type_value.setText(content[position]);
                        sp.setValue(MachineParameterSharedPreference.MACHINE_MACHINE_TYPE, String.valueOf(position));
                        if (position == 0 || position == 1) {
                            trans_type_layout.setVisibility(View.VISIBLE);
                            trans_type_time_layout.setVisibility(View.VISIBLE);
                            trans_type_count_layout.setVisibility(View.VISIBLE);
                        } else {
                            trans_type_layout.setVisibility(View.INVISIBLE);
                            trans_type_time_layout.setVisibility(View.INVISIBLE);
                            trans_type_count_layout.setVisibility(View.INVISIBLE);
                        }
                        if (position == 5) {
                            spinning_layout.setVisibility(View.VISIBLE);
                        } else {
                            spinning_layout.setVisibility(View.INVISIBLE);
                        }
                    }
                });
                listDialog.show(getFragmentManager(), "listDialog");
                break;
            }

            case R.id.btn_spinningin_type: {
                final String[] content = getResources().getStringArray(R.array.spinningin_type);
                ListDialogFragment listDialog = new ListDialogFragment();
                Bundle bd = new Bundle();
                bd.putStringArray(ListDialogFragment.ARRAY, content);
                listDialog.setArguments(bd);
                listDialog.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        spinningin_type_value.setText(content[position]);
                        sp.setValue(MachineParameterSharedPreference.MACHINE_SPINNINGIN_TYPE, String.valueOf(position));
                    }
                });
                listDialog.show(getFragmentManager(), "listDialog");
                break;
            }

            case R.id.btn_trans_type: {
                final String[] content = getResources().getStringArray(R.array.trans_type);
                ListDialogFragment listDialog = new ListDialogFragment();
                Bundle bd = new Bundle();
                bd.putStringArray(ListDialogFragment.ARRAY, content);
                listDialog.setArguments(bd);
                listDialog.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        trans_type_value.setText(content[position]);
                        sp.setValue(MachineParameterSharedPreference.MACHINE_TRANS_TYPE, String.valueOf(position));
                        byte[] datai = new byte[2];
                        datai[0] = (byte) ((position & 0xff00) >> 8);
                        datai[1] = (byte) (position & 0xff);
                        if (sp.getValue(MachineParameterSharedPreference.PLC_TYPE, "0").equals("0")) {
                            ModbusCommandManager.getInstance()
                                    .putPriorityCmd(CommandGenerator.createWriteCommand(
                                            (byte) 1, 3, datai));
                        } else {
                            FxCommandManager.getInstance()
                                    .putPriorityCmd(CommandGenerator.createWriteCommand(
                                            (byte) 1, 3, datai));
                        }

                    }
                });
                listDialog.show(getFragmentManager(), "listDialog");
                break;
            }
            case R.id.btn_direct_current: {
                final String[] content = getResources().getStringArray(R.array.yes_no_choose);
                ListDialogFragment listDialog = new ListDialogFragment();
                Bundle bd = new Bundle();
                bd.putStringArray(ListDialogFragment.ARRAY, content);
                listDialog.setArguments(bd);
                listDialog.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        direct_current_value.setText(content[position]);
                        sp.setValue(MachineParameterSharedPreference.MACHINE_DIRECT_CURRENT, String.valueOf(position));
                        int data = PLCData.getInstance().getHoldingRegister(1, 8);
                        data = Utility.BitUtils.setBitValue(data, 2, position);
                        byte[] datai = new byte[2];
                        datai[0] = (byte) ((data & 0xff00) >> 8);
                        datai[1] = (byte) (data & 0xff);
                        ModbusCommandManager.getInstance()
                                .putPriorityCmd(CommandGenerator.createWriteCommand(
                                        (byte) 1, 8, datai));
                    }
                });
                listDialog.show(getFragmentManager(), "listDialog");
                break;
            }
            case R.id.btn_empty_tube_belt_transport_type: {
                final String[] content = getResources().getStringArray(R.array.trans_type);
                ListDialogFragment listDialog = new ListDialogFragment();
                Bundle bd = new Bundle();
                bd.putStringArray(ListDialogFragment.ARRAY, content);
                listDialog.setArguments(bd);
                listDialog.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        empty_tube_belt_transport_type_value.setText(content[position]);
                        sp.setValue(MachineParameterSharedPreference.MACHINE_EMPTY_TUBE_BELT_TRANSPORT_TYPE, String.valueOf(position));
                        if (position == 0) {
                            empty_tube_belt_action_interval_layout.setVisibility(View.INVISIBLE);
                        } else {
                            empty_tube_belt_action_interval_layout.setVisibility(View.VISIBLE);
                        }
                        int data = PLCData.getInstance().getHoldingRegister(1, 8);
                        data = Utility.BitUtils.setBitValue(data, 0, position);
                        byte[] datai = new byte[2];
                        datai[0] = (byte) ((data & 0xff00) >> 8);
                        datai[1] = (byte) (data & 0xff);
                        ModbusCommandManager.getInstance()
                                .putPriorityCmd(CommandGenerator.createWriteCommand(
                                        (byte) 1, 8, datai));//723
                    }
                });
                listDialog.show(getFragmentManager(), "listDialog");
                break;
            }
            case R.id.btn_flat_belt_sensor: {
                final String[] content = getResources().getStringArray(R.array.have_none);
                ListDialogFragment listDialog = new ListDialogFragment();
                Bundle bd = new Bundle();
                bd.putStringArray(ListDialogFragment.ARRAY, content);
                listDialog.setArguments(bd);
                listDialog.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        flat_belt_sensor_value.setText(content[position]);
                        sp.setValue(MachineParameterSharedPreference.MACHINE_FLAT_BELT_SENSOR, String.valueOf(position));
                        int data = PLCData.getInstance().getHoldingRegister(1, 8);
                        data = Utility.BitUtils.setBitValue(data, 1, position);
                        byte[] datai = new byte[2];
                        datai[0] = (byte) ((data & 0xff00) >> 8);
                        datai[1] = (byte) (data & 0xff);
                        ModbusCommandManager.getInstance()
                                .putPriorityCmd(CommandGenerator.createWriteCommand(
                                        (byte) 1, 8, datai));//723
                    }
                });
                listDialog.show(getFragmentManager(), "listDialog");
                break;
            }
            case R.id.btn_stop_type: {
                final String[] content = getResources().getStringArray(R.array.stop_type);
                ListDialogFragment listDialog = new ListDialogFragment();
                Bundle bd = new Bundle();
                bd.putStringArray(ListDialogFragment.ARRAY, content);
                listDialog.setArguments(bd);
                listDialog.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        stop_type_value.setText(content[position]);
                        sp.setValue(MachineParameterSharedPreference.STOP_TYPE, String.valueOf(position));
                    }
                });
                listDialog.show(getFragmentManager(), "listDialog");
                break;
            }
            default:
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onHoldingRegisterEvent(PLCData.HoldingRegister holding) {
        int addr = holding.addr;
        int value = holding.value;
        switch (addr) {
            case 0:
                machine_cycle_num_edit.setText(String.valueOf(value / 2));
                break;
            case 1:
                interval_time_edit.setText(String.valueOf(value));
                break;
            case 2:
                minus_pressure_edit.setText(String.valueOf(value / 40));
                break;
            case 3: {
                final String[] content = getResources().getStringArray(R.array.trans_type);
                trans_type_value.setText(content[value]);
            }
            break;
            case 4:
                times_edit.setText(String.valueOf(value));
                break;
            case 5:
                time_edit.setText(String.valueOf(value / 60));
                break;
            case 8: {
                String[] content = getResources().getStringArray(R.array.trans_type);
                empty_tube_belt_transport_type_value.setText(content[value & 1]);
            }
            break;
            case 9:
                empty_tube_belt_action_interval_edit.setText(String.valueOf(value));
                break;
            case 12:
                fork_off_time_edit.setText(String.valueOf(value));
                break;
            case 13:
                fork_on_time_edit.setText(String.valueOf(value));
                break;
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDRegisterEvent(FxPLCData.DRegister dRegister) {
        int addr = dRegister.fxPLCData.getKey(dRegister);
        int value = dRegister.value;
        switch (addr) {
            case (2 << 16 | 720):
                machine_cycle_num_edit.setText(String.valueOf(value / 2));
                break;
            case (2 << 16 | 721):
                interval_time_edit.setText(String.valueOf(value));
                break;
            case (2 << 16 | 722):
                minus_pressure_edit.setText(String.valueOf(value / 40));
                break;
            case (2 << 16 | 723):
                final String[] content = getResources().getStringArray(R.array.trans_type);
                trans_type_value.setText(content[value]);
                break;
            case (2 << 16 | 724):
                times_edit.setText(String.valueOf(value));
                break;
            case (2 << 16 | 725):
                time_edit.setText(String.valueOf(value / 60));
                break;
        }

    }
}
